<?php
namespace Admin\Controller;
use Think\Controller;
class RegController extends Controller {
    public function login(){
        $this->display();
    }
    public function verify(){
		ob_clean();
        $config =    array(
            'fontSize'    =>    18,
            'length'      =>    3,
            'useNoise'    =>    false,
            'bg'=>array(229,229,229),
			'codeSet'=>'0123456789',
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }
    public function verify_check($code,$id){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
    public function loginD($username,$password,$verify){
        if(!$this->verify_check($verify)){
            alert(0,'验证码错误');
        }
        if($username=='' || $password==''){
            alert(0,'用户名和密码都不能为空');
        }
        $data=M("admin")->field('id,salt,name,password,username,department_id,type,last_login_date,login_count,created_at')->where(array('username'=>$username))->find();

		// dump($password);dump($data['salt']);
		// dump(md5($password.$data['salt']));
		// die;

        if(md5($password.$data['salt'])==$data['password']){
            session('admin',$username);
			session('admin_info',$data);
            session('type',$data['type']);//type=1是主管理员，最高权限不能删除
			
			$d=array();
            $d['last_login_date']=date("Y-m-d H:i:s",time());
            $d['login_count']=$data['login_count']+1;
            M("admin")->where(array('username'=>$username))->save($d);
            alert(1, '登录成功');
        }else{
            alert(0, '登录失败');
        }
    }
    public function exits(){
        session('admin',null);
        session('type',null);
        $this->redirect('Reg/login');
    }
}